﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;

using Matrix = Microsoft.Xna.Framework.Matrix;
using BlobTowerDefense.Managers;
using BlobTowerDefense.Tools;

namespace BlobTowerDefense.Game_Entities
{
    public class World
    {
        Model model;
        public TowerManager TowerManager;

        EnemyManager enemyManager;

        public World()
        {       
            model = null;

            this.TowerManager = new TowerManager();
            this.enemyManager = new EnemyManager();
        }

        public void LoadModel(string modelName)
        {
            model = GamePage.content.Load<Model>(modelName);

            Matrix[] AbsoluteBonesTransform = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(AbsoluteBonesTransform);
        }

        public Model getModel()
        {
            return model;
        }

        public void Update()
        {       
            if ((CursorState.IsTowerSelected) && (CursorState.Gesture.GestureType == GestureType.Tap))
            {
                Ray cursorRay = CursorState.CalculateCursorRay();

                int? towerSelectedIndex = this.TowerManager.SelectTower(cursorRay);
                if (towerSelectedIndex != null)
                {
                    TowerManager.BuildTower(towerSelectedIndex);
                    //CursorState.IsTowerSelected = false;
                    //CursorState.TowerSelectedStats = TowerManager.DefaultTowerStats[TOWERTYPE.buildingPlace];
                }
            }

            this.enemyManager.Update();

            this.TowerManager.Update(enemyManager);
        }

        public void Draw()
        {

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect currentEffect in mesh.Effects)
                {
                    currentEffect.EnableDefaultLighting();
                    currentEffect.Projection = Camera.ProjectionMatrix;
                    currentEffect.View = Camera.ViewMatrix;
                    currentEffect.World = Matrix.Identity;

                    foreach (EffectPass pass in currentEffect.CurrentTechnique.Passes)
                    {
                        pass.Apply();
                    }
                }
                mesh.Draw();
            }
  
            this.TowerManager.Draw();
            enemyManager.Draw();
            
        }

        
        /// <summary>
        /// Set view matrix
        /// </summary>
        /// <param name="View">View matrix</param>
        public void SetViewMatrix(Matrix View)
        {
            //Camera.ViewMatrix = View;
        }

        public WAVETYPE? GetCurrentWaveType()
        {
            if (this.enemyManager == null)
            {
                return null;
            }
            else
            {
                return this.enemyManager.GetCurrentWaveType();
            }
        }

        public bool LaunchNextWave()
        {
            bool toReturn = this.enemyManager.NextWave();
            this.enemyManager.LaunchWave();

            return toReturn;
        }

        public bool WaveRunning()
        {
            return this.enemyManager.WaveRunning();
        }

        public bool WaveEnded()
        {
            return this.enemyManager.WaveEnded();
        }

        public void SpecialAttack()
        {
            this.enemyManager.SpecialAttack();
        }

        public void UpgradeTowers()
        {
            this.TowerManager.UpgradeTowers();
        }
    }
}
